﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BusinessComponents;
using BusinessComponents.Managers;

namespace WaterfrontLodge.Validators
{
    public class ReservationValidator
    {
        public static void chkReservationPeriod(ReservationMaster master)
        {
            if (master.StartDate < DateTime.Now)
            {
                throw new ValidationException("StartDate", "Start Date must be one day after current date.");
            }
            else if (master.EndDate < DateTime.Now)
            {
                throw new ValidationException("EndDate", "End Date must be one day after current date.");
            }
            else if (master.EndDate < master.StartDate)
            {
                throw new ValidationException("EndDate", "End Date must not be earlier than Start Date.");
            }
            else
            {
                TimeSpan ts = master.EndDate - master.StartDate;
                master.DayCount = ts.Days + 1;
            }
        }

        public static void chkReservationRoom(ReservationMaster master)
        {
            if (master.RoomCount < 1)
            {
                throw new ValidationException("RoomCount", "No. of Rooms must not be less than 1.");
            }
            else if (master.NoOfPax < 1)
            {
                throw new ValidationException("NoOfPax", "No. of Pax must not be less than 1.");
            }
            else
            {
                RoomType rt = ManagerFactory.RoomTypeMgr.GetRoomType(master.TypeCode);
                int maxOccupants = master.RoomCount + rt.MaxOccupant;

                if (master.NoOfPax > maxOccupants)
                {
                    throw new ValidationException("NoOfPax", "No. of Pax must not exceed " + maxOccupants + ".");
                }
                else
                {
                    master.TotalCost = master.DayCount * master.RoomCount * rt.StdPrice;
                    master.RoomTypeDesc = rt.Description;
                }
            }
        }

        public static void chkReservationRemarks(ReservationMaster master)
        {
            string remarks = ("" + master.Remarks).Trim();
            if (remarks.Length > 256)
            {
                throw new ValidationException("Remarks", "Remarks must not exceed 256 characters.");
            }
            else
            {
                master.Remarks = remarks;
            }
        }

        public static bool chkIsReservationOwner(string reservationId, Guid userId)
        {
            return ManagerFactory.ReservationMgr.isOwner(reservationId, userId);
        }
    }
}